/**
 * 后台接口
 */

/**
 * 初始化数据
 */
function init_data() {
    $(function() {
        $(".userinfo .am-icon-sign-out").click(function() {
            ME.logout(function() {
                window.location.href = ME.loginFile + ".html";
            })
        });
        $(".userinfo .am-icon-unlock-alt").click(function() {
            Util.addPopUp("modify_password", "modify_password", 300);
        });
    
        // 今日统计信息
        var date = new Date().Format("yyyy-MM-dd");
        CMD.bet_sum_query("#SUM#", date, date, '#SUM#', function(r) {
            if (Util.isEmpty(r)) return;
            $(".userinfo .yl").html((r[0].profit/100).toFixed(2));
            // TODO: 其他信息
        });
    
        ME.query(function() {
            $(".welcome span").html(context.user.user.mobile);
        });
    
        $(".top .page-title a").click(function() {
            Util.jumpUrl({
                n1: '',
                n2: ''
            })
        });
    
        // 定期刷新顶部数据
        var _loop = function() {
            CMD.user_online_query({
                online: 1,
                pageNum: 1,
                pageSize: 1,
            }, function(r) {
                $(".userinfo .online").html(r.count);
            });
            CMD.account_log_state0(function(r) {
                $(".charge_count").html(r.deposit || 0);
                $(".take_count").html(r.withdraw || 0);
            });
            CMD.user_online_query({
                tail: 1,
                pageNum: 1,
                pageSize: 1,
            }, function(r) {
                $(".userinfo .online-tail").html(r.count);
            });
        }
        Util.registerTimer("top-stat", 5, _loop);
        _loop();
    });
}

// 填充输赢信息的数据
function fill_win_table(list, table) {
    table.empty();
    for (var i in list) {
        var tr = $("<tr></tr>");
        tr.append("<td>" + list[i].date + "</td>");
        tr.append("<td>" + get_game_display_name(list[i].type) + "</td>");
        tr.append("<td>" + (list[i].amount/100) + "</td>");

        if (list[i].profit > 0)
            tr.append("<td class='red'>" + (list[i].profit/100) + "</td>");
        else
            tr.append("<td class='green'>" + (list[i].profit/100) + "</td>");

        table.append(tr);
    }
}

/**
 * 添加一个开奖的结果项目 
 */
function add_result_item(title, c) {
    var l = 
    '<tr>' +
    '    <th>__title__</th>' +
    '    <td>' +
    '        <input type="number" class="am-form-field" placeholder="" />' +
    '    </td>' +
    '</tr>';
    l = l.replace(/__title__/, title);
    c.append(l);
}
function add_result_item_button(c) {
    if (!c) c = $(".form-table-without-line tr:last input").parent();
    c.append('<button type="button" class="save">保存</button>');
}

/**
 * 显示一个用户的详细信息
 */
function show_user_detail(user) {
    var f = function(o) {
        context.center.user = o;
        Util.addPopUp("user_detail_page", "user_detail_page", 440);
    };

    if (typeof(user) === 'string') {
        if (user === '游客') return;

        // 首选查找该用户的信息
        startLoading();
        CMD.find_user_by_name(user, function(r) {
            stopLoading();
            if (r.list.length == 0) return;
            f(r.list[0]);
        });
    }
    else f(user);
}

function lottery_ai(game) {
    var globalRate, userRate;
    function queryAi() {
        CMD.lottery_ai_query(game, function(r) {
            var list = r.list;
            for (var i in list) {
                var d = list[i];
                if (!d.uids) {
                    globalRate = d;
                    $(".global-rate").val(d.rate);
                    if (d.state == 1) {
                        $("select.mode").val(1);
                        $("select.mode").trigger('changed.selected.amui');
                    }
                }
                else {
                    userRate = d;
                    $(".user-rate").val(d.rate);
                    $(".user-list").val(d.uids);
                    if (d.state == 1) {
                        $("select.mode").val(2);
                        $("select.mode").trigger('changed.selected.amui');
                    }
                }
            }
        });
    }
    queryAi();
    $(".mode-save").click(function() {
        var mode = $("select.mode").val();
        if (mode == 1) {
            if (!globalRate) {
                alert2("请先设置全局杀率！", true);
                return;
            }
            startLoading();
            CMD.lottery_ai_state(globalRate.id, 1, function() {
                globalRate.state = 1;
                stopLoading();
            });
            if (userRate && userRate.state == 1) {
                userRate.state = 0;
                CMD.lottery_ai_state(userRate.id, 0);
            }
        }
        else if (mode == 2) {
            if (!userRate) {
                alert2("请先设置用户杀率！", true);
                return;
            }
            startLoading();
            CMD.lottery_ai_state(userRate.id, 1, function() {
                userRate.state = 1;
                stopLoading();
            });
            if (globalRate && globalRate.state == 1) {
                globalRate.state = 0;
                CMD.lottery_ai_state(globalRate.id, 0);
            }
        }
        else {
            if (globalRate && globalRate.state == 1) {
                globalRate.state = 0;
                CMD.lottery_ai_state(globalRate.id, 0);
            }
            if (userRate && userRate.state == 1) {
                userRate.state = 0;
                CMD.lottery_ai_state(userRate.id, 0);
            }
        }
    });
    $(".global-rate-save").click(function() {
        var v = $(".global-rate").val();
        if (!globalRate) globalRate = { rate: v, type: game };
        else globalRate.rate = v;
        startLoading();
        CMD.lottery_ai_save(globalRate, function(r) {
            stopLoading();
            queryAi();
            alert2("保存成功");
        })
    })
    $(".user-rate-save").click(function() {
        var v = $(".user-rate").val();
        var list = $(".user-list").val().trim();
        if (!userRate) userRate = { rate: v, type: game, uids: list, };
        else {
            userRate.rate = v;
            userRate.uids = list;
        }
        startLoading();
        CMD.lottery_ai_save(userRate, function(r) {
            stopLoading();
            queryAi();
            alert2("保存成功");
        })
    })
}
